package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Idea;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class IdeaDao {

	DBHelp<Idea> db=new DBHelp<Idea>();
	
	public void save(Idea idea){
		String sql="insert into t_idea(id,title,content,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		db.executeSQL(sql,idea.getId(),idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getEmployeeid(),idea.getProjectid());
	}
	
	public void delete(String id){
		String sql="delete from t_idea where id=?";
		db.executeSQL(sql,id);
	}
	
	public Idea findById(String id){
		String sql="select * from t_idea where id=?";
		return db.queryForObject(sql,new IRowMapper(),id);
	}
	
	public List<Idea> findByProjectid(String projectid){
		String sql="select * from t_idea where projectid=? order by createtime desc";
		return db.queryForList(sql,new IRowMapper(),projectid);
	}
	
	private class IRowMapper implements RowMapper<Idea>{

		public Idea mapperRow(ResultSet rs) throws SQLException {
			
			Idea idea=new Idea();
			idea.setId(rs.getString("id"));
			idea.setTitle(rs.getString("title"));
			idea.setContent(rs.getString("content"));
			idea.setCreatetime(rs.getString("createtime"));
			idea.setEmployeeid(rs.getInt("employeeid"));
			idea.setProjectid(rs.getString("projectid"));
			return idea;
			
		}
		
	}
}
